package com.latvia.ui;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.latvia.cookingapp.R;
import com.latvia.model.Recipe;

/**
 * Created by jevĢēnijs on 13.20.11.
 * go on Niggas
 */
public class AllRecipesActivity extends Activity {

    private List<Recipe> allRecipes = new ArrayList<Recipe>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_all_recipes);
        initiateRecipeList();
        populateListView();
//        getActionBar().setDisplayHomeAsUpEnabled(true);
    }

    private void initiateRecipeList() {
        allRecipes.clear();
        allRecipes.add(new Recipe("Pancakes", "The most delicious pancakes evaarrr!@!", 100, R.drawable.p4, 1, true, 1));
        allRecipes.add(new Recipe("Burger", "Burger from McDonalds", 50, R.drawable.p3, 1, true, 1));
        allRecipes.add(new Recipe("Burger2", "This burger is from Hesburger", 50, R.drawable.p2, 1, false, 2));
        allRecipes.add(new Recipe("Cake","Chocolate cake", 70, R.drawable.p1, 1, true, 3));
    }

    private void populateListView() {
        ArrayAdapter<Recipe> adapter = new RecipeListAdapter();
        ListView list = (ListView) findViewById(R.id.recipesListView);
        list.setAdapter(adapter);
    }

    private class RecipeListAdapter extends ArrayAdapter<Recipe> {
        public RecipeListAdapter() {
            super(AllRecipesActivity.this, R.layout.all_recipes_view, allRecipes);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // Make sure we have a view to work with (may have been given null)
            View itemView = convertView;
            if (itemView == null) {
                itemView = getLayoutInflater().inflate(R.layout.all_recipes_view, parent, false);
            }

            // Find the recipe to work with.
            Recipe recipe = allRecipes.get(position);

            // Fill the view
            ImageView imageView = (ImageView)itemView.findViewById(R.id.item_recipeIcon);
            imageView.setImageResource(recipe.getIconId());

            // Name:
            TextView makeText = (TextView) itemView.findViewById(R.id.item_recipeName);
            makeText.setText(recipe.getName());

            // Description:
            TextView yearText = (TextView) itemView.findViewById(R.id.item_recipeDescription);
            yearText.setText(recipe.getDescription());

            // Exp:
            TextView condionText = (TextView) itemView.findViewById(R.id.item_recipeXp);
            condionText.setText(recipe.getExperience().toString());

            return itemView;
        }
    }
}
